<?php

namespace app\controllers\h5\my;

use app\controllers\h5\base\H5Controller;
use app\models\user\UserCommission;

class CommissionController extends H5Controller
{
    public function actionList()
    {
        $status = -1;
        $type = intval($this->post('type'));

        if ($type == 1) {
            $status = 1;
        }
        if ($type == 2) {
            $status = 0;
        }

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = UserCommission::find()
            ->andWhere(['user_id' => $this->user->id]);

        if ($status > -1) {
            $query->andWhere(['status' => $status]);
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }
}